
import { Spin } from 'antd'
import React from 'react'
import ReactDOM from 'react-dom'
import './style.less'

const Loading = function () {
  this.timer = ''
}

Loading.prototype.show = function () {
  this.hide()
  this.timer = setTimeout(() => {
    const div = document.createElement('div')
    div.setAttribute('class', 'loading__wrap')
    ReactDOM.render(
      <div className='loading'>
        <Spin size='large' />
      </div>
      , div)

    document.body.appendChild(div)
  }, 20)

}

Loading.prototype.hide = function () {
  this.timer && clearTimeout(this.timer)
  this.clear()
}

Loading.prototype.clear = function () {
  try {
    const div = document.querySelectorAll('.loading__wrap')
    let i = div.length

    if (i < 1) return
    while (i--) {
      ReactDOM.unmountComponentAtNode(div[i])
      document.body.removeChild(div[i])
    }
  } catch (err) {
    return
  }
}

export default new Loading()